package com.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.UserBean;
import com.project.dao.UserDao;
import com.project.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, UserBean> implements UserService {
    @Override
    public UserBean login(String userName, String userPwd) {
        QueryWrapper<UserBean> wrapper = new QueryWrapper();
        wrapper.eq("user_name", userName);
        wrapper.eq("user_pwd", userPwd);
        UserBean userBean = this.getOne(wrapper);
        return userBean;
    }

    @Override
    public IPage<UserBean> findAllUser(int pageNO, int pageSize) {
        Page<UserBean> page = new Page<>(pageNO, pageSize);
        IPage<UserBean> iPage = this.page(page);
        return iPage;
    }

    @Override
    public void addUser(UserBean userBean) {
        List<UserBean> list = new ArrayList<>();
        for (UserBean i : list) {
            if (userBean.getUserName().equals(i.getUserName())) {
                return;
            }
        }
        this.baseMapper.addUser(userBean);
    }

    @Override
    public void delUser(int userId) {
        this.baseMapper.delUser(userId);
    }

    @Override
    public void updateUser(int userId, String userPwd, String userRood) {
        this.baseMapper.updateUser(userId, userPwd, userRood);
    }

    @Override
    public IPage<UserBean> findRood(int pageNO, int pageSize, String userRood) {
        Page<UserBean> page = new Page<>(pageNO, pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (userRood != null && !userRood.equals("")) {
            wrapper.eq("f_userGrade", userRood);
        }
        IPage<UserBean> iPage = this.page(page, wrapper);
        return iPage;
    }
}
